// pages/orderDeatil/orderDetail.js
import {getOrderDetail} from '../../api/orderDetail';

import {
  getWechatFlag,
  getUserInfo,
  pay
} from '../../api/public'
import {
  wechatPay
} from '../../api/settlement'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusList:['待支付','待送车','使用中','已完成','已关闭'],
    id:"",
    detail:{},
    time:'',
    payFlag:false,
    userInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = options.id;
    this.getDetail();
  },
  apply() {
    wx.showLoading({
      title: '加载中...',
    })
    getUserInfo({}).then(e => {
      this.setData({
        userInfo: e.data.userInfo,
        payFlag: false
      })
      if (this.data.userInfo.memberLevel == 0 || !this.data.userInfo.memberLevel) {
        pay(1, this.data.id).then(e=>{
          this.getDetail();
        })
      } else {
        this.setData({
          payFlag: true
        })
        wx.hideLoading()
      }
    })
  },
  copyOrderNum(e){
    const id = e.currentTarget.dataset.id;
    wx.setClipboardData({
      data: id,
      success: function (res) {
        
          wx.showToast({
            title: '复制成功',
          })
        
      }
    })
  },
  add(m){return m<10?'0'+m:m },
  gettime(params, node) {
    
      //shijianchuo是整数，否则要parseInt转换
      var time = new Date(params);
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      if(node == 'mm'){
        return `${this.add(h)}:${this.add(mm)}`
      }
      if(node == 'day'){
        return `${y}-${this.add(m)}-${this.add(d)}`
      }
      return y+'-'+this.add(m)+'-'+this.add(d)+' '+this.add(h)+':'+this.add(mm);
      
  },
  getDetail(){
    wx.showLoading({
      title: '加载中...',
    })
    getOrderDetail({
      data:{
        id:this.data.id
      }
    }).then(e=>{
      wx.hideLoading()
      let times = JSON.stringify(e.data.pick_up_time)
      e.data.pick_up_time = this.gettime(e.data.pick_up_time,'day')
      e.data.drop_off_time = this.gettime(e.data.drop_off_time,'day')
      e.data.created_at = this.gettime(e.data.created_at)
      
      // if(e.data.payment_at){
      //   e.data.payment_at = this.gettime(e.data.payment_at)
      // }
      this.setData({
        detail:e.data,
        time:this.gettime(JSON.parse(times),'mm')
      })
      console.log(this.data.detail,'dededed-------------------')
      
    })
  },
  judgeMember() {
    wx.showLoading({
      title: '加载中...',
    })
    getUserInfo({}).then(e => {
      this.setData({
        userInfo: e.data.userInfo,
        payFlag: false
      })
      if (this.data.userInfo.memberLevel == 0) {
        pay(1, this.data.id).then(e=>{
          this.getDetail();
        })
      } else {
        this.setData({
          payFlag: true
        })
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onApply(e){
   this.getDetail()
  },
  sendOrder(){
    wechatPay({
      data:{
        order_id:this.data.detail.id
      }
    })
    .then(r=>{
      wx.requestPayment({
        timeStamp:r.timestamp,
        nonceStr:r.nonceStr,
        package:r.package,
        signType:r.signType,
        paySign:r.paySign,
        success (res) {
          console.log(res,'成功')
        },
        fail (res){
          console.log(res,'失败')
        }
      })
    })
  }
})